package cn.iocoder.boot.service.impl;

import cn.iocoder.boot.common.RedisKeyConstants;
import cn.iocoder.boot.dto.SuppliersQueryTO;
import cn.iocoder.boot.entity.ComponentStock;
import cn.iocoder.boot.entity.ComponentSupplier;
import cn.iocoder.boot.entity.KbPlatformApi;
import cn.iocoder.boot.entity.ReplaceRelationManagement;
import cn.iocoder.boot.mapper.ComponentStockMapper;
import cn.iocoder.boot.mapper.ComponentSupplierMapper;
import cn.iocoder.boot.service.IComponentSupplierService;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.jsoup.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.serviceException;
import static com.baomidou.mybatisplus.extension.toolkit.Db.*;

@Service
public class ComponentSupplierServiceImpl implements IComponentSupplierService {

    @Autowired
    private ComponentSupplierMapper baseMapper;

    @Resource
    private RedisTemplate redisTemplate;
    @Override
    public boolean saveComponentSupplier(ComponentSupplier ComponentSupplier) {
         baseMapper.insert(ComponentSupplier);
        return true;
    }

    @Override
    public boolean updateComponentSupplier(ComponentSupplier ComponentSupplier) {
        return updateById(ComponentSupplier);
    }

    @Override
    public boolean deleteComponentSupplier(Long id) {
         baseMapper.deleteById(id);
         return true;
    }

    @Override
    public ComponentSupplier getComponentSupplier(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public Page<ComponentSupplier> listComponentSuppliers(SuppliersQueryTO to) {
        Page<ComponentSupplier> page = new Page<>(to.getPageNo(), to.getPageSize());


        LambdaQueryWrapper<ComponentSupplier> queryWrapper = new LambdaQueryWrapper<>();

        // 根据不同的条件，拼接查询
        if (!StringUtil.isBlank(to.getNo())) {
            queryWrapper.eq(ComponentSupplier::getNo, to.getNo());
        }
        if (to.getIsSupportApi() != null) {
            queryWrapper.eq(ComponentSupplier::getIsSupportApi, to.getIsSupportApi());
        }
        if (to.getIsSupportQuery() != null) {
            queryWrapper.eq(ComponentSupplier::getIsSupportQuery, to.getIsSupportQuery());
        }
        if (to.getIsSupportOrder() != null) {
            queryWrapper.eq(ComponentSupplier::getIsSupportOrder, to.getIsSupportOrder());
        }

        // 确保查询的是未删除的记录
        queryWrapper.eq(ComponentSupplier::getDeleted, 0);
        queryWrapper.orderByAsc(ComponentSupplier::getPriority);
        Page<ComponentSupplier> parentList = baseMapper.selectPage(page, queryWrapper);

        return parentList;
    }

    @Override
    public List<ComponentSupplier> getList() {
        LambdaQueryWrapper<ComponentSupplier> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ComponentSupplier::getDeleted, 0);
        return baseMapper.selectList(queryWrapper);



    }

    //缓存取限制，更快
    public Long getLimit(String code) {

        String redisKey = String.format(RedisKeyConstants.SUPPLIER_LIMIT, code);
        Object o = redisTemplate.opsForValue().get(redisKey);

        if (Objects.isNull(o)) {
            //查询数据库

            LambdaQueryWrapper<ComponentSupplier> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ComponentSupplier::getNo, code);
            queryWrapper.eq(ComponentSupplier::getDeleted, 0);
            ComponentSupplier supplier = baseMapper.selectOne(queryWrapper);

            if (Objects.isNull(supplier)) {
                throw serviceException("供应商不存在，请联系管理员");
            }
            if(supplier.getDayLimit()==0){
                redisTemplate.opsForValue().set(redisKey, supplier.getDayLimit(), 2, TimeUnit.DAYS);
                throw serviceException("供应商当日次数已用完，请联系管理员");
            }

            //设置缓存
            redisTemplate.opsForValue().set(redisKey, supplier.getDayLimit(), 2, TimeUnit.DAYS);
            return supplier.getDayLimit();
        }else{
            Long limit = Long.valueOf((Integer)o) ;

            if(limit==0){
                throw serviceException("供应商当日次数已用完，请联系管理员");
            }

            return limit;
        }





    }

    public void reset() {



// 执行更新操作
        baseMapper.reset();




    }
}
